package com.gpstracker.overlays;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.gpstracker.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import com.gpstracker.db.MyDatabase;
import com.gpstracker.services.OverlayHandler;

@SuppressWarnings("rawtypes")
public class MyItemizedOverlay extends ItemizedOverlay {

	private static final String tag = "MY_ITEMIZED_OVERLAY";
	
	private int time_limit_trip;
	private OverlayHandler overlayHandler;
	
	private ArrayList<OverlayItem> mOverlays;
	Context mContext;

	public MyItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		mOverlays = new ArrayList<OverlayItem>();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		time_limit_trip = Integer.parseInt(prefs.getString("timeRange", "300"));
		if (time_limit_trip < 60)
			time_limit_trip = 60;
		else 
			time_limit_trip *= 5;  //it's equal to *6;
		populate();
	}

	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		mOverlays = new ArrayList<OverlayItem>();
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}
	
	public void clear(){
		mOverlays.clear();
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

//	@Override
//	protected boolean onTap(int index) {
//		OverlayItem item = mOverlays.get(index);
//		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
//		dialog.setTitle(item.getTitle());
//		dialog.setMessage(item.getSnippet());
//		dialog.show();
//		return true;
//	}
	
	public void setHandler(OverlayHandler overlayHandler){
		this.overlayHandler = overlayHandler;
	}
	
	@Override
	protected boolean onTap(int index) {
		final OverlayItem item = mOverlays.get(index);
		
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.dialog);
		dialog.setTitle(item.getTitle());

		// set the custom dialog components - text, image and button
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText(item.getSnippet());
		
		Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MyDatabase db=new MyDatabase(mContext);
				db.open();
				
				String[] splitted=item.getTitle().split("\n");
				
				db.delete(splitted[0].substring(14));
				db.close();
				dialog.dismiss();
				mOverlays.clear();
				overlayHandler.fill();
				
			}
		});

		dialog.show();
		
		
		return true;
	}
	
  

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);

		

		int R = 100;
		int G = 0;
		int B = 0;
		
		if(!mOverlays.isEmpty())
			for (int i=0; i < mOverlays.size()-1; i++ ) {

				
				
				
				//FARE CONTROLLO DELLA SOGLIA QUI E POI DISABILITARE GLI OVERLAY VISUALIZZATI
				Integer time1 = Integer.parseInt(mOverlays.get(i).getTitle().substring(mOverlays.get(i).getTitle().length()-10));
				Integer time2 = Integer.parseInt(mOverlays.get(i+1).getTitle().substring(mOverlays.get(i+1).getTitle().length()-10));

				if(time2 - time1 <= time_limit_trip){

					GeoPoint point1 = mOverlays.get(i).getPoint(); 
					GeoPoint point2 = mOverlays.get(i+1).getPoint();

					// Let's assume you've assigned values to these two GeoPoints now.

					Projection projection = mapView.getProjection();
					Point startingPoint = projection.toPixels(point1, null);
					Point endingPoint = projection.toPixels(point2, null);

					// Create the path containing the line between the two points.
					Path path = new Path();
					path.moveTo(startingPoint.x, startingPoint.y);
					path.lineTo(endingPoint.x, endingPoint.y);

					// Setup the paint.  You'd probably do this outside of the draw() method to be more efficient.
					Paint paint = new Paint();
					paint.setStyle(Paint.Style.STROKE);
					paint.setStrokeWidth(5);
					paint.setARGB(100, R, G, B);
					// Can set other paint characteristics, such as width, anti-alias, color, etc....

					// Draw the path!
					canvas.drawPath(path, paint);
				}
				else{

					R += 30;
					R = R % 255;
					
					G += 100;
					G = G % 255;
					
					B += 50;
					B = B % 255;
				}
			}
	}


}
